package com.menhai.personmanagement.controller;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.menhai.personmanagement.entity.ReportHealthrecord;
import com.menhai.personmanagement.entity.vo.HealthCheckVo;
import com.menhai.personmanagement.entity.vo.HealthRecordVo;
import com.menhai.personmanagement.services.ReportHealthRecordService;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import lombok.extern.slf4j.Slf4j;
/**
 * <p>
 * 报卡管理表 前端控制器
 * </p>
 *
 * @author shikun
 * @since 2021-11-25
 */

@Slf4j
@RestController
@RequestMapping("/healthRecord")
public class HealthRecordController {

     @Autowired
     private ReportHealthRecordService reportHealthRecordService;

     //新建健康记录
     @RequestMapping("/add")
     public Object addReportHealthrecord( ReportHealthrecord reportHealthrecord) throws Exception{
		 Map<String, Object> map = null;
		 //新建时间
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 reportHealthrecord.setCreateTime(sdf.format(new Date()));
		 //健康记录
		 reportHealthrecord.setHealthRecordNum(sdf.format(new Date()).replace("-", "").replace(":", "").replace(" ", ""));
		 //新增慢病报卡
		 map = reportHealthRecordService.addReportHealthrecord(reportHealthrecord);
		 return map;
     }
     
     
     
     
   /**
   * 查询健康记录列表
   * @param request
   * @param response
   * @param healthCheckVo
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/getHealthRecordInfoList", method = RequestMethod.POST)
	public Object getHealthRecordInfoList(HttpServletRequest request, HttpServletResponse response,HealthRecordVo healthRecordVo) throws Exception {
	  	String hosnumarray = request.getParameter("hosnumarray");
	  	healthRecordVo.setHosnumArray(hosnumarray.split(","));
	  	healthRecordVo.setParamStr(hosnumarray);
  		IPage<HealthRecordVo> list = reportHealthRecordService.getHealthRecordInfoList(healthRecordVo);
		return list;
	}
  
  

	/**
	 * 根据体检id查询信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getOneById", method = RequestMethod.POST)
	public Object getOneById(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//记录id
		String id = request.getParameter("id");
		return reportHealthRecordService.getOneById(id);
	}

     
     
	/**
	 * 编辑记录
	 * @param reportHealthrecord
	 * @return
	 * @throws Exception
	 */
     @RequestMapping("/edit")
     public Object updateHealthRecord(ReportHealthrecord reportHealthrecord) throws Exception{
 		 Map<String, Object> map = null;
 		 //修改时间
 		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 		reportHealthrecord.setUpdateTime(formatter.format(new Date()));
 		 //编辑信息
 		 map = reportHealthRecordService.updateHealthRecord(reportHealthrecord);
 		 return map;
     }
     
     /**
      * frontTwoFlag
      * @param request
      * @param response
      * @return
      * @throws Exception
      */
     @RequestMapping("/getFrontTwo")
     public Object getFrontTwo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		 String frontTwoFlag = request.getParameter("frontTwoFlag");
		 return reportHealthRecordService.getFrontTwo(frontTwoFlag);
     }
     
     
     
     //导出excel
     @RequestMapping("/exportExcel")
     public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception{
     	String id = request.getParameter("id");
     	Map<String, String> healthRecord = reportHealthRecordService.getHealthRecord(id);
     	ExcelWriter writer = ExcelUtil.getWriter();
     	//去掉头部背景颜色
     	StyleSet styleSet = writer.getStyleSet();
     	styleSet.setBackgroundColor(IndexedColors.WHITE, true);
     	//内容字体大小
     	Font fontContent = writer.createFont();
     	writer.setDefaultRowHeight(25);
     	fontContent.setFontHeight(Short.parseShort("240"));
     	CellStyle cellStyleContent = styleSet.getHeadCellStyle();
     	cellStyleContent.setFont(fontContent);
     	int rowNum=3;
     	
     	//表格开始
     	List<String> header1 = new LinkedList<>();
     	header1.add("老年人中医药健康管理服务记录表");
     	header1.add("");
     	header1.add("");
     	header1.add("");
     	header1.add("");
     	header1.add("");
     	header1.add("");
     	header1.add("");
     	header1.add("");
     	header1.add("");
     	header1.add("");
     	writer.writeHeadRow(header1);
     	writer.merge(0,0,0,10,null,false);
     	//第一行表名字体样式
     	Font font = writer.createFont();
     	font.setBold(true);
     	font.setFontHeight(Short.parseShort("400"));
     	CellStyle cellStyle = writer.getCellStyle();
     	cellStyle.setFont(font);
     	Cell cell = writer.getCell(0, 0);
     	cell.setCellStyle(cellStyle);
     	
     	List<String> header2 = new LinkedList<>();
     	header2.add("姓名:");
     	header2.add(nullToString(healthRecord.get("name")));
     	header2.add("");
     	header2.add("");
     	header2.add("");
     	header2.add("");
     	header2.add("");
     	header2.add("");
     	header2.add("");
     	header2.add("编号:");
     	header2.add(nullToString(healthRecord.get("healthRecordNum")));
     	writer.writeHeadRow(header2);
     	writer.merge(1,1,2,8,null,false);
     	
     	List<String> header3 = new LinkedList<>();
     	header3.add("请根据近一年的体验和感觉，回答以下问题");
     	header3.add("");
     	header3.add("");
     	header3.add("");
     	header3.add("");
     	header3.add("没有");
     	header3.add("很少");
     	header3.add("有时");
     	header3.add("经常");
     	header3.add("总是");
     	header3.add("总分");
     	writer.writeHeadRow(header3);
     	writer.merge(2,3,0,4,null,false);
     	
     	List<String> header4 = new LinkedList<>();
     	header4.add("");
     	header4.add("");
     	header4.add("");
     	header4.add("");
     	header4.add("");
     	header4.add("(根本不/从来没有)");
     	header4.add("(有一点/偶尔)");
     	header4.add("(有些/少数时间)");
     	header4.add("(相当/多数时间)");
     	header4.add("(非常/每天)");
     	header4.add("");
     	writer.writeHeadRow(header4);
     	writer.merge(2,3,10,10,null,false);
     	//问题列表
     	List<Map<String, Object>> mould = reportHealthRecordService.getMould();
     	for(int i=0;i<mould.size();i++){
     		rowNum++;
     		Map<String, Object> map = mould.get(i);
     		List<String> header = new LinkedList<>();
     		if(map.get("option1").toString().contains("@@")||map.get("option2").toString().contains("@@")||map.get("option3").toString().contains("@@")||map.get("option4").toString().contains("@@")||map.get("option5").toString().contains("@@")){
     			header.add("("+map.get("id").toString()+")"+map.get("qname").toString());
     			header.add("");
     	     	header.add("");
     	     	header.add("");
     	     	header.add("");
     	     	String string = healthRecord.get("q"+(i+1));
     	     	for(int x=1;x<6;x++){
     	     		if(StringUtils.isNotEmpty(string)&&x==Integer.parseInt(string)){
     	     			header.add(string);
     	     		}else{
     	     			header.add("");
     	     		}
     	     	}
     	     	header.add(string);
     	     	writer.writeHeadRow(header);
     	     	writer.merge(rowNum,rowNum+1,0,4,null,false);
     	     	//合并行
     	     	List<String> headerSpan = new LinkedList<>();
     	     	headerSpan.add("");
     	     	headerSpan.add("");
     	     	headerSpan.add("");
     	     	headerSpan.add("");
     	     	headerSpan.add("");
     	     	
     	     	headerSpan.add(map.get("option1").toString().split("@@")[1]);
     	     	headerSpan.add(map.get("option2").toString().split("@@")[1]);
     	     	headerSpan.add(map.get("option3").toString().split("@@")[1]);
     	     	headerSpan.add(map.get("option4").toString().split("@@")[1]);
     	     	headerSpan.add(map.get("option5").toString().split("@@")[1]);
     	     	headerSpan.add("");
     	     	writer.writeHeadRow(headerSpan);
     	     	writer.merge(rowNum,rowNum+1,10,10,null,false);
     	     	rowNum++;
     		}else{
     			header.add("("+map.get("id").toString()+")"+map.get("qname").toString());
     			header.add("");
     	     	header.add("");
     	     	header.add("");
     	     	header.add("");
     	     	String string = healthRecord.get("q"+(i+1));
     	     	for(int x=1;x<6;x++){
     	     		if(StringUtils.isNotEmpty(string)&&x==Integer.parseInt(string)){
     	     			header.add(string);
     	     		}else{
     	     			header.add("");
     	     		}
     	     	}
     	     	header.add(string);
     	     	writer.writeHeadRow(header);
     	     	writer.merge(rowNum,rowNum,0,4,null,false);
     		}
     	}
     	List<String> header5 = new LinkedList<>();
     	header5.add("体质类型");
     	header5.add("");
     	header5.add("气虚质");
     	header5.add("阳虚质");
     	header5.add("阴虚质");
     	header5.add("痰湿质");
     	header5.add("湿热质");
     	header5.add("血瘀质");
     	header5.add("气郁质");
     	header5.add("特禀质");
     	header5.add("平和质");
     	writer.writeHeadRow(header5);
     	writer.merge(rowNum+1,rowNum+1,0,1,null,false);
     	
     	List<String> header6 = new LinkedList<>();
     	header6.add("体质辨识");
     	header6.add("");
     	header6.add("1．得分  "+nullToString(healthRecord.get("score1")));
     	header6.add("1．得分 "+nullToString(healthRecord.get("score2")));
     	header6.add("1．得分  "+nullToString(healthRecord.get("score3")));
     	header6.add("1．得分  "+nullToString(healthRecord.get("score4")));
     	header6.add("1．得分  "+nullToString(healthRecord.get("score5")));
     	header6.add("1．得分  "+nullToString(healthRecord.get("score6")));
     	header6.add("1．得分  "+nullToString(healthRecord.get("score7")));
     	header6.add("1．得分  "+nullToString(healthRecord.get("score8")));
     	header6.add("1．得分  "+nullToString(healthRecord.get("score9")));
     	writer.writeHeadRow(header6);
     	writer.merge(rowNum+2,rowNum+5,0,1,null,false);
     	
     	List<String> header7 = new LinkedList<>();
     	header7.add("");
     	header7.add("");
     	header7.add("2．是"+(nullToString(healthRecord.get("tz1")).equals("1")?"√":""));
     	header7.add("2．是 "+(nullToString(healthRecord.get("tz2")).equals("1")?"√":""));
     	header7.add("2．是"+(nullToString(healthRecord.get("tz3")).equals("1")?"√":""));
     	header7.add("2．是 "+(nullToString(healthRecord.get("tz4")).equals("1")?"√":""));
     	header7.add("2．是"+(nullToString(healthRecord.get("tz5")).equals("1")?"√":""));
     	header7.add("2．是"+(nullToString(healthRecord.get("tz6")).equals("1")?"√":""));
     	header7.add("2．是"+(nullToString(healthRecord.get("tz7")).equals("1")?"√":""));
     	header7.add("2．是"+(nullToString(healthRecord.get("tz8")).equals("1")?"√":""));
     	header7.add("2．是"+(nullToString(healthRecord.get("tz9")).equals("1")?"√":""));
     	writer.writeHeadRow(header7);
     	
     	List<String> header8 = new LinkedList<>();
     	header8.add("");
     	header8.add("");
     	header8.add("3．倾向是"+(nullToString(healthRecord.get("tz1")).equals("2")?"√":""));
     	header8.add("3．倾向是"+(nullToString(healthRecord.get("tz2")).equals("2")?"√":""));
     	header8.add("3．倾向是"+(nullToString(healthRecord.get("tz3")).equals("2")?"√":""));
     	header8.add("3．倾向是"+(nullToString(healthRecord.get("tz4")).equals("2")?"√":""));
     	header8.add("3．倾向是"+(nullToString(healthRecord.get("tz5")).equals("2")?"√":""));
     	header8.add("3．倾向是"+(nullToString(healthRecord.get("tz6")).equals("2")?"√":""));
     	header8.add("3．倾向是"+(nullToString(healthRecord.get("tz7")).equals("2")?"√":""));
     	header8.add("3．倾向是"+(nullToString(healthRecord.get("tz8")).equals("2")?"√":""));
     	header8.add("3．基本是"+(nullToString(healthRecord.get("tz9")).equals("2")?"√":""));
     	writer.writeHeadRow(header8);
     	
     	List<String> header8_1 = new LinkedList<>();
     	header8_1.add("");
     	header8_1.add("");
     	header8_1.add("4．否"+(nullToString(healthRecord.get("tz1")).equals("3")?"√":""));
     	header8_1.add("4．否"+(nullToString(healthRecord.get("tz2")).equals("3")?"√":""));
     	header8_1.add("4．否"+(nullToString(healthRecord.get("tz3")).equals("3")?"√":""));
     	header8_1.add("4．否"+(nullToString(healthRecord.get("tz4")).equals("3")?"√":""));
     	header8_1.add("4．否"+(nullToString(healthRecord.get("tz5")).equals("3")?"√":""));
     	header8_1.add("4．否"+(nullToString(healthRecord.get("tz6")).equals("3")?"√":""));
     	header8_1.add("4．否"+(nullToString(healthRecord.get("tz7")).equals("3")?"√":""));
     	header8_1.add("4．否"+(nullToString(healthRecord.get("tz8")).equals("3")?"√":""));
     	header8_1.add("4．否"+(nullToString(healthRecord.get("tz9")).equals("3")?"√":""));
     	writer.writeHeadRow(header8_1);
     	//------------------------------------------
     	List<String> header9 = new LinkedList<>();
     	header9.add("中医药保健指导");
     	header9.add("");
     	header9.add("1．情志调摄"+(nullToString(healthRecord.get("zd1")).contains("zd11")?"√":""));
     	header9.add("1．情志调摄"+(nullToString(healthRecord.get("zd2")).contains("zd21")?"√":""));
     	header9.add("1．情志调摄"+(nullToString(healthRecord.get("zd3")).contains("zd31")?"√":""));
     	header9.add("1．情志调摄"+(nullToString(healthRecord.get("zd4")).contains("zd41")?"√":""));
     	header9.add("1．情志调摄"+(nullToString(healthRecord.get("zd5")).contains("zd51")?"√":""));
     	header9.add("1．情志调摄"+(nullToString(healthRecord.get("zd6")).contains("zd61")?"√":""));
     	header9.add("1．情志调摄"+(nullToString(healthRecord.get("zd7")).contains("zd71")?"√":""));
     	header9.add("1．情志调摄"+(nullToString(healthRecord.get("zd8")).contains("zd81")?"√":""));
     	header9.add("1．情志调摄"+(nullToString(healthRecord.get("zd9")).contains("zd91")?"√":""));
     	writer.writeHeadRow(header9);
     	writer.merge(rowNum+6,rowNum+11,0,1,null,false);
     	
     	List<String> header10 = new LinkedList<>();
     	header10.add("");
     	header10.add("");
     	header10.add("2．饮食调养"+(nullToString(healthRecord.get("zd1")).contains("zd12")?"√":""));
     	header10.add("2．饮食调养"+(nullToString(healthRecord.get("zd2")).contains("zd22")?"√":""));
     	header10.add("2．饮食调养"+(nullToString(healthRecord.get("zd3")).contains("zd32")?"√":""));
     	header10.add("2．饮食调养"+(nullToString(healthRecord.get("zd4")).contains("zd42")?"√":""));
     	header10.add("2．饮食调养"+(nullToString(healthRecord.get("zd5")).contains("zd52")?"√":""));
     	header10.add("2．饮食调养"+(nullToString(healthRecord.get("zd6")).contains("zd62")?"√":""));
     	header10.add("2．饮食调养"+(nullToString(healthRecord.get("zd7")).contains("zd72")?"√":""));
     	header10.add("2．饮食调养"+(nullToString(healthRecord.get("zd8")).contains("zd82")?"√":""));
     	header10.add("2．饮食调养"+(nullToString(healthRecord.get("zd9")).contains("zd92")?"√":""));
     	writer.writeHeadRow(header10);
     	
     	List<String> header11 = new LinkedList<>();
     	header11.add("");
     	header11.add("");
     	header11.add("3．起居调摄"+(nullToString(healthRecord.get("zd1")).contains("zd13")?"√":""));
     	header11.add("3．起居调摄"+(nullToString(healthRecord.get("zd2")).contains("zd23")?"√":""));
     	header11.add("3．起居调摄"+(nullToString(healthRecord.get("zd3")).contains("zd33")?"√":""));
     	header11.add("3．起居调摄"+(nullToString(healthRecord.get("zd4")).contains("zd43")?"√":""));
     	header11.add("3．起居调摄"+(nullToString(healthRecord.get("zd5")).contains("zd53")?"√":""));
     	header11.add("3．起居调摄"+(nullToString(healthRecord.get("zd6")).contains("zd63")?"√":""));
     	header11.add("3．起居调摄"+(nullToString(healthRecord.get("zd7")).contains("zd73")?"√":""));
     	header11.add("3．起居调摄"+(nullToString(healthRecord.get("zd8")).contains("zd83")?"√":""));
     	header11.add("3．起居调摄"+(nullToString(healthRecord.get("zd9")).contains("zd93")?"√":""));
     	writer.writeHeadRow(header11);
     	
     	List<String> header12 = new LinkedList<>();
     	header12.add("");
     	header12.add("");
     	header12.add("4．运动保健"+(nullToString(healthRecord.get("zd1")).contains("zd14")?"√":""));
     	header12.add("4．运动保健"+(nullToString(healthRecord.get("zd2")).contains("zd24")?"√":""));
     	header12.add("4．运动保健"+(nullToString(healthRecord.get("zd3")).contains("zd34")?"√":""));
     	header12.add("4．运动保健"+(nullToString(healthRecord.get("zd4")).contains("zd44")?"√":""));
     	header12.add("4．运动保健"+(nullToString(healthRecord.get("zd5")).contains("zd54")?"√":""));
     	header12.add("4．运动保健"+(nullToString(healthRecord.get("zd6")).contains("zd64")?"√":""));
     	header12.add("4．运动保健"+(nullToString(healthRecord.get("zd7")).contains("zd74")?"√":""));
     	header12.add("4．运动保健"+(nullToString(healthRecord.get("zd8")).contains("zd84")?"√":""));
     	header12.add("4．运动保健"+(nullToString(healthRecord.get("zd9")).contains("zd94")?"√":""));
     	writer.writeHeadRow(header12);
     	
     	List<String> header13 = new LinkedList<>();
     	header13.add("");
     	header13.add("");
     	header13.add("5．穴位保健"+(nullToString(healthRecord.get("zd1")).contains("zd15")?"√":""));
     	header13.add("5．穴位保健"+(nullToString(healthRecord.get("zd2")).contains("zd25")?"√":""));
     	header13.add("5．穴位保健"+(nullToString(healthRecord.get("zd3")).contains("zd35")?"√":""));
     	header13.add("5．穴位保健"+(nullToString(healthRecord.get("zd4")).contains("zd45")?"√":""));
     	header13.add("5．穴位保健"+(nullToString(healthRecord.get("zd5")).contains("zd55")?"√":""));
     	header13.add("5．穴位保健"+(nullToString(healthRecord.get("zd6")).contains("zd65")?"√":""));
     	header13.add("5．穴位保健"+(nullToString(healthRecord.get("zd7")).contains("zd75")?"√":""));
     	header13.add("5．穴位保健"+(nullToString(healthRecord.get("zd8")).contains("zd85")?"√":""));
     	header13.add("5．穴位保健"+(nullToString(healthRecord.get("zd9")).contains("zd95")?"√":""));
     	writer.writeHeadRow(header13);
     	
     	List<String> header14 = new LinkedList<>();
     	header14.add("");
     	header14.add("");
     	header14.add("6．其他： "+nullToString(healthRecord.get("zdqt1")));
     	header14.add("6．其他： "+nullToString(healthRecord.get("zdqt2")));
     	header14.add("6．其他： "+nullToString(healthRecord.get("zdqt3")));
     	header14.add("6．其他： "+nullToString(healthRecord.get("zdqt4")));
     	header14.add("6．其他： "+nullToString(healthRecord.get("zdqt5")));
     	header14.add("6．其他： "+nullToString(healthRecord.get("zdqt6")));
     	header14.add("6．其他： "+nullToString(healthRecord.get("zdqt7")));
     	header14.add("6．其他： "+nullToString(healthRecord.get("zdqt8")));
     	header14.add("6．其他： "+nullToString(healthRecord.get("zdqt9")));
     	writer.writeHeadRow(header14);
     	
     	
     	List<String> header15 = new LinkedList<>();
     	header15.add("填表日期");
     	header15.add("");
     	header15.add(nullToString(healthRecord.get("recordDate")));
     	header15.add("");
     	header15.add("");
     	header15.add("医生签名");
     	header15.add("");
     	header15.add("");
     	header15.add(nullToString(healthRecord.get("doctorName")));
     	header15.add("");
     	header15.add("");
     	writer.writeHeadRow(header15);
     	writer.merge(rowNum+12,rowNum+12,0,1,null,false);
     	writer.merge(rowNum+12,rowNum+12,2,4,null,false);
     	writer.merge(rowNum+12,rowNum+12,5,7,null,false);
     	writer.merge(rowNum+12,rowNum+12,8,10,null,false);
     	
     	//调整列宽度
     	writer.setColumnWidth(0,20);     	
     	writer.setColumnWidth(1,20);
     	writer.setColumnWidth(2,20);
     	writer.setColumnWidth(3,20);
     	writer.setColumnWidth(4,20);     	
     	writer.setColumnWidth(5,20);
     	writer.setColumnWidth(6,20);
     	writer.setColumnWidth(7,20);
     	writer.setColumnWidth(8,20);     	
     	writer.setColumnWidth(9,20);
     	writer.setColumnWidth(10,20);

   
          response.setContentType("application/vnd.ms-excel;charset=utf-8");
          String fileName = new String("老年人中医药健康管理服务记录表".getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1);
          response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
          ServletOutputStream out = null;
          try {
               out = response.getOutputStream();
               writer.flush(out, true);
          } catch (IOException e) {
               e.printStackTrace();
          } finally {
               // 关闭writer，释放内存
               writer.close();
          }
          //此处记得关闭输出Servlet流
          IoUtil.close(out);
     }
     
     
     
     public  String nullToString(String param){
     	if(StringUtils.isNotEmpty(param)){
     		return param;
     	}else{
     		return "";
     	}
     }
     
     
     /**
 	 * 左侧树
 	 * @param hosnum
 	 * @return
 	 */
    @RequestMapping("/getCunRenShu")
    public List<Map<String,Object>> getCunRenShu(HttpServletRequest request, HttpServletResponse response){
 	    //医院编号
 	    String hosnum = request.getParameter("hosnum");
        return reportHealthRecordService.getCunRenShu(hosnum);
    }

    
}
